package cn.com.surker.handler;

import cn.com.surker.base.strength.entity.BaseStrengthSpecial;
import cn.com.surker.base.strength.service.StrengthSpecialBaseService;
import cn.com.surker.enums.StateEnum;
import cn.com.surker.production.entity.ProductionIndent;
import cn.com.surker.production.entity.ProductionInvoice;
import cn.com.surker.production.entity.vo.ProductionInvoiceVo;
import cn.com.surker.production.service.ErpIndentService;
import cn.com.surker.production.service.ErpInvoiceService;
import cn.com.surker.redis.RedisUtils;
import cn.com.surker.sale.entity.SaleStrengthSpecialPrice;
import cn.com.surker.sale.service.SaleStrengthSpecialPriceBaseService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 打印操作
 *
 * @author luotao
 * @date 2025/4/15
 */
@Slf4j
@Component
public class PrintHandler {

	@Value("${erp.org.id}")
	private Long orgId;
	@Value("${erp.org.code}")
	private String orgCode;
	@Value("${erp.datasource.erp.db}")
	private String erpDb;
	/**
	 * 用于调度打印的key
	 */
	public static final String disPrintKey = "ERP_DISPATCH_PRINT_REDIS_KEY";

	/**
	 * 过磅打印
	 */
	public static final String weightPrintKey = "ERP_WEIGHT_PRINT_REDIS_KEY";

	@Autowired
	private ErpInvoiceService invoiceService;
	@Autowired
	private ErpIndentService indentService;
	@Autowired
	private SaleStrengthSpecialPriceBaseService specialPriceService;

	@Resource
	private RedisUtils redisUtils;

	/**
	 * 将打印数据添加到待打印队列
	 *
	 * @param printKey
	 * @param invoiceId
	 */
	public void addInvoice(String printKey, Long invoiceId) {
		if (invoiceId == null) {
			System.err.println("未选择发货单");
			return;
		}
		//	查询发货单数据
		ProductionInvoiceVo invoiceVo = invoiceService.findPumpCodeDetail(erpDb, invoiceId);
		if (invoiceVo == null) {
			System.err.println("未查询出发货单信息," + erpDb + "," + orgId + "," + invoiceId);
			return;
		}
		//	之前是否打印过
		Integer printNum = invoiceVo.getPrintNum();
		if (printNum == null || printNum == 0) {
			//	之前没有打印过
			printNum = 1;
		} else {
			//	之前打印过
			printNum++;
		}
		// invoiceVo.setPrintNum(printNum);
		//	更新发货单数据
		UpdateWrapper<ProductionInvoice> uw = new UpdateWrapper<>();
		uw.eq("id", invoiceId);
		uw.set("print_num", printNum);
		invoiceService.update(uw);
		//	查询发货单是否需要分开打印
		@NotNull(message = "请选择任务单") Long indentId = invoiceVo.getIndentId();
		ProductionIndent indent = indentService.getById(indentId);
		//	销售合同
		Long conId = indent.getConId();
		//	强度等级
		Long strengthId = indent.getStrengthId();
		//	特性要求
		String techIdsStr = indent.getTechId();
		//	强度等级
		String strengthName = invoiceVo.getStrengthName();
		//	特性要求字符
		String techStr = indent.getTech();
		if (StringUtils.isNotBlank(techIdsStr)) {
			//	有特性要求
			String[] techArr = techIdsStr.split(",");
			for (String techIdStr : techArr) {
				//	特性要求
				Long techId = Long.valueOf(techIdStr);
				//	查询特性要求
				SaleStrengthSpecialPrice one = specialPriceService.getById(techId);
				if (one != null) {
					String equirement = one.getEquirement();
					Integer isPrint = one.getIsPrint();
					if (isPrint != null && isPrint == 0) {
						//	不需要分开打印
						// strengthName += equirement;
						continue;
					} else {
						//	需要分开打印
						strengthName = strengthName.replaceAll(equirement, "");
						techStr = techStr.replaceAll(equirement, "");
						invoiceVo.addRemark(equirement);
					}
				}
			}
		}
		invoiceVo.setStrengthName(strengthName);
		invoiceVo.setTech(techStr);
		//	存入打印队列
		redisUtils.leftPush(printKey, invoiceVo);
	}
}
